<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if = "!isActive">
      <slot name="item-icon"></slot>
    </div>

    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>

    <div :style="activeStyle">
      <slot name="item-text" ></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props:{
    path: String,
    activeColor: {
      type: String,
      default: 'red'
    }
  },
  data(){
    return{
      // isActive: false
      count:1,
    }
  },
  computed: {
    isActive(){
      // let ok = this.$route.path.indexOf(this.path)!=-1
      // alert(this.$route.path+"="+this.path+this.count+ok)
      // this.count += this.count++
      //当前活跃路径 不包含 当前传入路径 = -1 false
      //当前活跃路径 包含 当前传入路径 = 0 true
      //问题:初始化调用  显示8次 第5次时count = 2 直到结束，每次切换显示4次 count切换一次增加1 是计算属性自带的缓存嘛？
      return this.$route.path.indexOf(this.path)!=-1
    },
    activeStyle() {
      return this.isActive ? {'color': 'red'} : {}
    }
  },
  methods:{
    itemClick(){
        this.$router.push(this.path)
    },
    okffw(){
      let ok = this.$route.path.indexOf(this.path)!=-1
      alert(this.$route.path+"="+this.path+this.count+ok)
      this.count++
      //用此方法代替计算属性 进入了死循环？
      return this.$route.path.indexOf(this.path)!=-1
    }
  }

}
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 11px;
  }
  .tab-bar-item img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    /*vertical-align: middle;*/
  }

</style>
